package selef.xiafan.dao;

import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import selef.xiafan.entity.system.SysPermission;

import java.util.List;
import java.util.Optional;

/**
 * @author xiafan
 * @version 1.0
 * @date 2021/7/1 23:11
 */
public interface SysPermissionDao extends JpaRepository<SysPermission, Integer> {

    List<SysPermission> findAllByParentId(Integer parentId);

    @Query(value = "SELECT COUNT(*) FROM t_sys_permission", nativeQuery = true)
    Long countAll();

    @Query(value = "SELECT * FROM t_sys_permission LIMIT :page,:size", nativeQuery = true)
    List<SysPermission> findAllNoQuery(@Param("page") Integer page, @Param("size") Integer size);

    SysPermission findByName(String name);

    Optional<SysPermission> findById(Integer parentId);

    void deleteById(Integer id);

    SysPermission findByUrl(String url);


    List<SysPermission> findAllById(Integer permissionId);

    @Query(value = "SELECT * FROM t_sys_permission WHERE name_zh LIKE CONCAT('%',:nameZhForQuery,'%') LIMIT :page,:size ",nativeQuery = true)
    List<SysPermission> findAllByQuery(@Param("page") Integer page, @Param("size") Integer size, @Param("nameZh") String nameZhForQuery);

    @Query(value = "SELECT COUNT(*) FROM t_sys_permission WHERE name_zh LIKE CONCAT('%',:nameZhForQuery,'%')", nativeQuery = true)
    Long countAllByQuery(@Param("nameZh") String nameZhForQuery);
}
